import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:startapp/util/view.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  GlobalKey _key = GlobalKey<FormState>();
  TextEditingController _userController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  FocusNode _userFocusNode = FocusNode();
  FocusScopeNode? _focusScopeNode;
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _userController.dispose();
    _passwordController.dispose();
    if (_focusScopeNode != null) {
      _focusScopeNode!.dispose();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppbar("登录"),
      body: Column(
        children: [
          Image.asset("images/independentmind.jpeg",
              width: double.infinity, height: 260, fit: BoxFit.fill),
          SizedBox(
            height: 16,
          ),
          Form(
              key: _key,
              child: Column(
                children: [
                  TextFormField(
                    focusNode: _userFocusNode,
                    onFieldSubmitted: (v) {},
                    textInputAction: TextInputAction.next,
                    autofocus: true,
                    controller: _userController,
                    validator: (v) {
                      if (v == null || v.isEmpty) {
                        return "请输入用户！";
                      }
                    },
                    decoration: InputDecoration(
                        icon: Icon(Icons.account_circle),
                        labelText: "user",
                        hintText: 'your account'),
                  ),
                  SizedBox(
                    height: 8,
                  ),
                  TextFormField(
                    controller: _passwordController,
                    textInputAction: TextInputAction.next,
                    decoration: InputDecoration(
                      icon: Icon(Icons.lock),
                      labelText: "password",
                      hintText: 'your parssword',
                    ),
                    validator: (v) {
                      if (v == null || v.isEmpty) {
                        return "请输入密码！";
                      }
                    },
                    obscureText: true,
                  ),
                  SizedBox(
                    height: 16,
                  ),
                  ElevatedButton(
                    onPressed: () {
                      if ((_key.currentState as FormState).validate()) {
                        print("true");
                        Navigator.of(context).pushNamed("/");
                      } else {
                        if (_focusScopeNode == null) {
                          _focusScopeNode = FocusScope.of(context);
                        }
                        _focusScopeNode!.requestFocus(_userFocusNode);
                        print("false");
                      }
                    },
                    child: Text("登录"),
                  )
                ],
              ))
        ],
      ),
    );
  }
}
